package fun.shuofeng.juc.countdownlatch;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;

public class CountdownLatchDemo01 {

    static Logger logger = Logger.getLogger(CountdownLatchDemo01.class.getSimpleName());

    public static void main(String[] args) throws InterruptedException {
        // test01();
        test02();
    }

    private static void test02() {

    }


    private static void test01() throws InterruptedException {
        CountDownLatch latch = new CountDownLatch(3);
        logger.info("...begin");
        new Thread(() -> {
            try {
                logger.info("t1 start...");
                TimeUnit.SECONDS.sleep(1);
                latch.countDown();
                logger.info("t1 end...");
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }).start();

        new Thread(() -> {
            try {
                logger.info("t2 start...");
                TimeUnit.SECONDS.sleep(2);
                latch.countDown();
                logger.info("t2 end...");
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }).start();

        new Thread(() -> {
            try {
                logger.info("t3 start...");
                TimeUnit.SECONDS.sleep(3);
                latch.countDown();
                logger.info("t3 end...");
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }).start();

        latch.await();
        logger.info("...end");
    }
}
